package com.example.mylock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * description: UnsafeThread <br>
 * date: 2020/7/8 10:39 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class UnsafeThread {

    private static int num = 0;

    private static CountDownLatch countDownLatch = new CountDownLatch(10);

    private static Lock lock = new MyLock();

    /**
     * 自增
     */
    public static void inCreate(){
        //lock.lock();
        num++;
        //lock.unlock();
    }

    public static void test(){
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 100; j++) {
                    inCreate();
                    try {
                        Thread.sleep(20L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //每次执行完调用countDown
                countDownLatch.countDown();
            }).start();
        }

        while (true){
            if (countDownLatch.getCount() == 0){
                System.out.println(num);
                break;
            }
        }

    }

}
